local skel = fk.CreateSkill {
  name = "emo__pingjian",
}

Fk:loadTranslationTable{
  ["emo__pingjian"] = "评鉴",
  [":emo__pingjian"] = "出牌阶段限三次，你可以观看五张武将牌，为其中一张选择正确的称号，若答对，随机获得其一个技能直到你下回合开始。",

  ["#emo__pingjian"] = "评鉴:观看5张武将牌，仅有1个称号正确，选择正确称号以获得技能",
  ["#emo__pingjian-choice"] = "评鉴：请选择正确的称号！",
  ["#EMOpingjianReply"] = "%from 认为 %arg 的称号是 %arg2 !",
  ["#EMOpingjianResuilt"] = "%from 评鉴 %arg",
  ["#EMOpingjianError"] = "<font color='red'><b>开启武将较少，无法进行评鉴</b></font>",
  ["success"] = "成功",
  ["fail"] = "失败",

  ["$ty__pingjian1"] = "识人读心，评荐推达。",
  ["$ty__pingjian2"] = "月旦雅评，试论天下。",
}

skel:addEffect("active", {
  anim_type = "drawcard",
  card_num = 0,
  target_num = 0,
  prompt = "#emo__pingjian",
  card_filter = Util.FalseFunc,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) < 3
  end,
  times = function (self, player)
    return 3 - player:usedSkillTimes(self.name, Player.HistoryPhase)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    -- 获取称号
    local getName = function (g)
      local name = Fk:translate("#"..g, "zh_CN")
      if name ~= "#"..g and name ~= "" then
        return name
      end
      return nil
    end
    -- 读取称号库
    local map = room:getTag("emo__pingjian_data")
    if map == nil then
      map = { {}, {} }
      for _, general in ipairs(room.general_pile) do
        local name = getName(general)
        if name then
          table.insert(map[1], general)
          table.insert(map[2], name)
        end
      end
      room:setTag("emo__pingjian_data", map)
    end
    local generals = map[1]
    local names = map[2]
    if #generals < 5 or #names < 9 then
      room:sendLog{ type = "#EMOpingjianError", toast = true }
      return nil
    end
    local general
    -- 进行评鉴
    generals = table.random(generals, 5)
    for _, g in ipairs(generals) do
      table.removeOne(names, getName(g))
    end
    local index = math.random(5)
    local true_general = generals[index]
    local true_name = getName(true_general)
    names = table.random(names, 4)
    table.insert(names, index, true_name)
    local result = room:askToCustomDialog(player, {
      qml_path = "packages/emopack/qml/PingjianBox.qml",
      skill_name = skel.name, extra_data = {
        generals, names, "#emo__pingjian-choice"
      }
    })
    if result ~= "" then
      general = generals[table.indexOf(names, result)]
      if general then
        room:sendLog{ type = "#EMOpingjianReply", from = player.id, arg = Fk:translate(general), arg2 = result, toast = true }
      end
    end
    -- 执行奖励内容
    local success = result == true_name
    room:delay(400)
    room:sendLog{ type = "#EMOpingjianResuilt", from = player.id, arg = success and "success" or "fail", toast = true }
    room:delay(300)
    if success then
      local skills = table.filter(Fk.generals[general]:getSkillNameList(), function (s)
        return not player:hasSkill(s, true)
      end)
      if #skills > 0 then
        local skill = table.random(skills)
        room:addTableMark(player, "emo__pingjian_skill", skill)
        room:handleAddLoseSkills(player, skill)
      end
    else
      -- 失败
      room:setEmotion(player, "./image/anim/judgebad")
    end
  end,
})

skel:addEffect(fk.TurnStart, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:getMark("emo__pingjian_skill") ~= 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local skills = player:getMark("emo__pingjian_skill")
    local room = player.room
    room:setPlayerMark(player, "emo__pingjian_skill", 0)
    room:handleAddLoseSkills(player, "-"..table.concat(skills, "|-"))
  end,
})

return skel
